/*
 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_CLONE_INSTALLER_H
#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_CLONE_INSTALLER_H

#include <string>

#include "bundle_app_spawn_client.h"
#include "bundle_data_mgr.h"
#include "inner_bundle_info.h"
#include "event_report.h"

namespace OHOS {
namespace AppExecFwk {
class BundleCloneInstaller final {
public:
    BundleCloneInstaller();
    ~BundleCloneInstaller();

    /**
     * @brief The main function for clone app installing.
     * @param bundleName Indicates the bundleName of the clone application to install.
     * @param userId Indicates the clone app will be installed under which user id.
     * @param appIndex Indicates the appIndex generated by the clone application.
     * @return Returns ERR_OK if the clone app install successfully; returns error code otherwise.
     */
    ErrCode InstallCloneApp(const std::string &bundleName, const int32_t userId, int32_t &appIndex);
    /**
     * @brief The main function for clone app uninstalling.
     * @param bundleName Indicates the bundleName of the clone application to uninstall.
     * @param appIndex Indicates the clone app index.
     * @param userId Indicates the clone app will be uninstalled under which user id.
     * @return Returns ERR_OK if the clone app uninstall successfully; returns error code otherwise.
     */
    ErrCode UninstallCloneApp(const std::string &bundleName,
        const int32_t userId, const int32_t appIndex, bool sync);
    /**
     * @brief The main function for clone app uninstalling.
     * @param bundleName Indicates the bundleName of the clone applications to uninstall.
     * @return Returns ERR_OK if the clone apps uninstall successfully; returns error code otherwise.
     */
    ErrCode UninstallAllCloneApps(const std::string &bundleName, bool sync,
        int32_t userId = Constants::INVALID_USERID);

private:
    ErrCode CreateCloneDataDir(
        InnerBundleInfo &info, const int32_t userId, const int32_t &uid, const int32_t &appIndex) const;
    ErrCode RemoveCloneDataDir(const std::string bundleName, int32_t userId, int32_t appIndex, bool sync);
    void CreateEl5Dir(InnerBundleInfo &info, const int32_t userId, const int32_t uid, const int32_t appIndex);
    void RemoveEl5Dir(InnerBundleUserInfo &userInfo, const int32_t uid, int32_t userId, const int32_t appIndex);
    ErrCode GetDataMgr();
    void UninstallDebugAppSandbox(const std::string &bundleName, const int32_t uid, int32_t appIndex,
        const InnerBundleInfo& innerBundleInfo);

    ErrCode ProcessCloneBundleInstall(const std::string &bundleName, const int32_t userId, int32_t &appIndex);
    ErrCode ProcessCloneBundleUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex, bool sync);

    void SendBundleSystemEvent(const std::string &bundleName, BundleEventType bundleEventType,
        int32_t userId, int32_t appIndex, bool isPreInstallApp, bool isFreeInstallMode,
        InstallScene preBundleScene, ErrCode errCode);
    void GetCallingEventInfo(EventInfo &eventInfo);

    void ResetInstallProperties();

    std::string GetAssetAccessGroups(const std::string &bundleName);
    std::string GetDeveloperId(const std::string &bundleName);

    int32_t uid_ = 0;
    uint32_t accessTokenId_ = 0;
    uint32_t versionCode_ = 0;
    bool isBundleCrossAppSharedConfig_ = false;
    std::string appId_;
    std::string appIdentifier_;
    std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr;
};
} // AppExecFwk
} // OHOS

#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_SANDBOX_INSTALLER_H